var utils = require("../../../utils/utils.js");
Page({
    data: {
        formats: {},
        readOnly: false,
        placeholder: '温馨提示：请先选择文字效果后在此输入文字',
        editorHeight: 300,
        keyboardHeight: 0,
        isIOS: false,
        dirction: 'h',
        colors: ['#ffffff', '#000000', '#e73b3b', '#f25e8c', '#fb7e76', '#fcc02d', '#b28adb', '#2fcc86', '#47b1f7'],
        showAdv: false,
        bgImg: [{
                src: '/img/fontImg/1.png',
                preSrc: '/img/fontImg/2.png',
                select: false,
                type:'b'
            },
            {
                src: '/img/fontImg/3.png',
                preSrc: '/img/fontImg/4.png',
                select: false,
                type:'n'
            },
            {
                src: '/img/fontImg/5.png',
                preSrc: '/img/fontImg/6.png',
                select: false,
                type:'tl'
            },
            {
                src: '/img/fontImg/7.png',
                preSrc: '/img/fontImg/8.png',
                select: false,
                type:'tc'
            },
            {
                src: '/img/fontImg/9.png',
                preSrc: '/img/fontImg/10.png',
                select: false,
                type:'tr'
            }
        ],
        sliderSet:{
            min:20,
            max:160,
            value:160
        },
        bgcolor:'#ffffff',
        titleTxt:{
            fontSize:160,
            color:'#000000',
            text:'示例标题',
            align:'center',
            fontWight:'normal'
        },
        contentTxt:{
            fontSize:90,
            color:'#000000',
            text:'示例文字多行输入，超出打印在下一张',
            align:'center',
            fontWight:'normal'
        },
        setIndex:'',
        sizeMaix:3.5

    },
    goPrint(){
        var str = '<style>body{background-color:'+this.data.bgcolor+';padding:100px 16px 0 16px;box-sizing:border-box}</style><div style="font-size:'+this.data.titleTxt.fontSize+'px;text-align:'+this.data.titleTxt.align+';color:'+this.data.titleTxt.color+';font-weight:'+this.data.titleTxt.fontWight+';margin-top:10.027855153203342px">'+this.data.titleTxt.text+'</div><div style="font-size:'+this.data.contentTxt.fontSize+'px;text-align:'+this.data.contentTxt.align+';color:'+this.data.contentTxt.color+';font-weight:'+this.data.contentTxt.weight+';margin-top:60px">'+this.data.contentTxt.text+'</div>'
        var that = this;
         that.selectComponent("#loading").showLoadingCus('加密上传中');
        utils.uploadfile('','speedPrint.html','','data:text/html;base64,'+utils.baseEncode(str),that.data.dirction=='h'?'2':'1', function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            wx.navigateTo({
                url: '../list/list?code=' + that.data.code + '&name=文字速印&path=' + res.data.data.printUrl + '&pageNum=' + res.data.data.pageTotal +'&fileUrl='+res.data.data.fileUrl+'&serviceMainId='+that.data.serviceMainId+'&serviceSubtypeId='+that.data.serviceSubtypeId+'&tagFloor='+that.data.tagFloor+'&orientation='+res.data.data.orientation
              })
        })
    },
    changeGs(e){
        var type = e.currentTarget.dataset.r;
        if(this.data.setIndex=='t'){
            if(type=='b'){
                this.setData({['titleTxt.fontWight']:'bolder','bgImg[0].select':true,'bgImg[1].select':false})
            }
            if(type=='n'){
                this.setData({['titleTxt.fontWight']:'normal','bgImg[0].select':false,'bgImg[1].select':true})
            }
            if(type=='tl'){
                this.setData({['titleTxt.align']:'left','bgImg[2].select':true,'bgImg[3].select':false,'bgImg[4].select':false})
            }
            if(type=='tc'){
                this.setData({['titleTxt.align']:'center','bgImg[2].select':false,'bgImg[3].select':true,'bgImg[4].select':false})
            }
            if(type=='tr'){
                this.setData({['titleTxt.align']:'right','bgImg[2].select':false,'bgImg[3].select':false,'bgImg[4].select':true})
            }
        }
        if(this.data.setIndex=='c'){
            if(type=='b'){
                this.setData({['contentTxt.fontWight']:'bolder','bgImg[0].select':true,'bgImg[1].select':false})
            }
            if(type=='n'){
                this.setData({['contentTxt.fontWight']:'normal','bgImg[0].select':false,'bgImg[1].select':true})
            }
            if(type=='tl'){
                this.setData({['contentTxt.align']:'left','bgImg[2].select':true,'bgImg[3].select':false,'bgImg[4].select':false})
            }
            if(type=='tc'){
                this.setData({['contentTxt.align']:'center','bgImg[2].select':false,'bgImg[3].select':true,'bgImg[4].select':false})
            }
            if(type=='tr'){
                this.setData({['contentTxt.align']:'right','bgImg[2].select':false,'bgImg[3].select':false,'bgImg[4].select':true})
            }
        }
    },
    chooseBgColor(e){
        var color = e.currentTarget.dataset.color;
        this.setData({bgcolor:color})
    },
    chooseFontColor(e){
        var color = e.currentTarget.dataset.color;
        if(this.data.setIndex=='t'){
            this.setData({['titleTxt.color']:color})
        }
        if(this.data.setIndex=='c'){
            this.setData({['contentTxt.color']:color})
        }
    },
    textareaInt(e){
        this.setData({['contentTxt.text']:e.detail.value})
    },
    titleInput(e){
        this.setData({['titleTxt.text']:e.detail.value})
    },
    openFix(e){
        this.setData({'bgImg[0].select':false,'bgImg[1].select':false,'bgImg[2].select':false,'bgImg[3].select':false,'bgImg[4].select':false})
        var index = e.currentTarget.dataset.index;
        var size = e.currentTarget.dataset.size;
        var align = e.currentTarget.dataset.align;
        var weight = e.currentTarget.dataset.weight;
        var fIndex = 0;
        var aIndex = 0;
        switch (align){
            case 'center' :  aIndex = 3 ; break;
            case 'left' : aIndex = 2; break;
            case 'right': aIndex = 4;break;
        }
        switch (weight){
            case 'normal':fIndex = 1;break;
            case 'bolder':fIndex = 0;break;
        }
        let g =  'bgImg[' + fIndex + '].select';
        let x =  'bgImg[' + aIndex + '].select';
        this.setData({setIndex:index,showAdv:true,sliderSet:{min:20,max:index=='t'?180:120,value:size},[g]:true,[x]:true})
    },
    touchNo(){

    },
    slider1change(e){
        if(this.data.setIndex=='t'){
            this.setData({['sliderSet.value']:e.detail.value,['titleTxt.fontSize']:e.detail.value})
        }
        if(this.data.setIndex=='c'){
            this.setData({['sliderSet.value']:e.detail.value,['contentTxt.fontSize']:e.detail.value})
        }
    },
    closeFix(){
        this.setData({showAdv:false})
    },
    onLoad(e) {
        // const platform = wx.getSystemInfoSync().platform
        // const isIOS = platform === 'ios'
        // this.setData({
        //     isIOS
        // })
        // const that = this
        // this.updatePosition(0)
        // let keyboardHeight = 0
        // wx.onKeyboardHeightChange(res => {
        //     if (res.height === keyboardHeight) return
        //     const duration = res.height > 0 ? res.duration * 1000 : 0
        //     keyboardHeight = res.height
        //     setTimeout(() => {
        //         wx.pageScrollTo({
        //             scrollTop: 0,
        //             success() {
        //                 that.updatePosition(keyboardHeight)
        //                 that.editorCtx.scrollIntoView()
        //             }
        //         })
        //     }, duration)

        // })
        var that = this;
        that.setData({
            tagFloor:e.tagFloor,
            code:e.code,
            serviceMainId:e.serviceMainId,
            serviceSubtypeId:e.serviceSubtypeId
        })
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68',
                        lineH: 128
                    })
                } else {
                    that.setData({
                        pdbottom: '40',
                        lineH: 100
                    })
                }
            },
        })
        setTimeout(() => {
            wx.createSelectorQuery().select('.bdfix').boundingClientRect().exec((res) => {
                let { windowWidth } = wx.getSystemInfoSync()
                console.log(wx.getSystemInfoSync())
                let { windowHeight } = wx.getSystemInfoSync()
                let { devicePixelRatio } = wx.getSystemInfoSync()
                let [con] = res
                var bottomH = con.height * (750 / windowWidth);
                var midH = windowHeight  * (750 / windowWidth) - bottomH - 80 - 64 - 32 ;
                var pagesizeS = {
                    w:midH/1.414,
                    h:midH,
                }
                var pixMix = windowWidth*devicePixelRatio;
                var pagesizeH = {
                    w:pixMix>=750?'588':midH*1.414,
                    h:pixMix>=750?'416':midH
                }
                that.setData({pageSizeH:pagesizeH,pagesizeS:pagesizeS})
            })
          }, 0);
    },
    updatePosition(keyboardHeight) {
        const toolbarHeight = 50
        const {
            windowHeight,
            platform
        } = wx.getSystemInfoSync()
        let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
        this.setData({
            editorHeight,
            keyboardHeight
        })
    },
    calNavigationBarAndStatusBar() {
        const systemInfo = wx.getSystemInfoSync()
        const {
            statusBarHeight,
            platform
        } = systemInfo
        const isIOS = platform === 'ios'
        const navigationBarHeight = isIOS ? 44 : 48
        return statusBarHeight + navigationBarHeight
    },
    onEditorReady() {
        const that = this
        wx.createSelectorQuery().select('#editor').context(function (res) {
            console.log('1')
            that.editorCtx = res.context
            that.editorCtx.format('fontFamily', '微软雅黑')
            that.editorCtx.format('fontSize', '16px')
        }).exec()
    },
    blur() {
        this.editorCtx.blur()
    },
    format(e) {
        let {
            name,
            value
        } = e.target.dataset
        if (!name) return
        // console.log('format', name, value)
        this.editorCtx.format(name, value)


    },
    changeDeriction(e) {
        this.setData({
            dirction: e.target.dataset.index
        })
    },
    inbutchange(e) {
        console.log(e)
    },
    onStatusChange(e) {
        const formats = e.detail
        this.setData({
            formats
        })

    },
    // insertDivider() {
    //   this.editorCtx.insertDivider({
    //     success: function () {
    //       console.log('insert divider success')
    //     }
    //   })
    // },
    // clear() {
    //   this.editorCtx.clear({
    //     success: function (res) {
    //       console.log("clear success")
    //     }
    //   })
    // },
    // removeFormat() {
    //   this.editorCtx.removeFormat()
    // },
    // insertDate() {
    //   const date = new Date()
    //   const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    //   this.editorCtx.insertText({
    //     text: formatDate
    //   })
    // },
    // insertImage() {
    //   const that = this
    //   wx.chooseImage({
    //     count: 1,
    //     success: function (res) {
    //       that.editorCtx.insertImage({
    //         src: res.tempFilePaths[0],
    //         data: {
    //           id: 'abcd',
    //           role: 'god'
    //         },
    //         width: '80%',
    //         success: function () {
    //           console.log('insert image success')
    //         }
    //       })
    //     }
    //   })
    // }
})